#include "../../include/viewer/Skybox.h"


Skybox::Skybox(Vecteur v,int taille){
this->position=v;
this->taille=taille;
}

Skybox::Skybox(){
this->position=Vecteur();
this->taille=500;
}

//methodes
float Skybox::getX(){
  return position.getX();
}
float Skybox::getY(){
  return position.getY();
}
float Skybox::getZ(){
  return position.getZ();
}

void Skybox::chargerTextures(string typeCiel){

int i;
string path=string("textures/skybox/");
string tampon;
path += typeCiel;
tampon = path + string("/devant.jpg");
this->skytext[0] = loadTexture(tampon.c_str());
tampon = path + string("/droite.jpg");
this->skytext[1] = loadTexture(tampon.c_str());
tampon = path + string("/derriere.jpg");
this->skytext[2] = loadTexture(tampon.c_str());
tampon = path + string("/gauche.jpg");
this->skytext[3] = loadTexture(tampon.c_str());
tampon = path + string("/haut.jpg");
this->skytext[4] = loadTexture(tampon.c_str());
tampon = path + string("/bas.jpg");
this->skytext[5] = loadTexture(tampon.c_str());


//permet de ne pas voir les liaisons
for(i=0;i<=5;++i)
	{
		glBindTexture(GL_TEXTURE_2D, skytext[i]);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, 0x812F);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, 0x812F);
	}

}

void Skybox::afficher(){
	glColor3ub(255,255,255);
	 glScalef ( 500, 500, 500 );

	 //glDisable(GL_DEPTH_TEST);
  	 glDepthMask(GL_FALSE);

       glBindTexture(GL_TEXTURE_2D, skytext[0]);

       glBegin(GL_QUADS);
       glTexCoord2d(0,1);  glVertex3d(1,1,1);
       glTexCoord2d(0,0);  glVertex3d(1,1,-1);
       glTexCoord2d(1,0);  glVertex3d(-1,1,-1);
       glTexCoord2d(1,1);  glVertex3d(-1,1,1);
       glEnd();

	   glBindTexture(GL_TEXTURE_2D, skytext[1]);

	   glBegin(GL_QUADS);
       glTexCoord2d(0,1);  glVertex3d(1,-1,1);
       glTexCoord2d(0,0);  glVertex3d(1,-1,-1);
       glTexCoord2d(1,0);  glVertex3d(1,1,-1);
       glTexCoord2d(1,1);  glVertex3d(1,1,1);
       glEnd();

       glBindTexture(GL_TEXTURE_2D, skytext[2]);

       glBegin(GL_QUADS);
       glTexCoord2d(0,1);  glVertex3d(-1,-1,1);
       glTexCoord2d(0,0);  glVertex3d(-1,-1,-1);
       glTexCoord2d(1,0);  glVertex3d(1,-1,-1);
       glTexCoord2d(1,1);  glVertex3d(1,-1,1);
       glEnd();

       glBindTexture(GL_TEXTURE_2D, skytext[3]);

       glBegin(GL_QUADS);
       glTexCoord2d(0,1);  glVertex3d(-1,1,1);
       glTexCoord2d(0,0);  glVertex3d(-1,1,-1);
       glTexCoord2d(1,0);  glVertex3d(-1,-1,-1);
       glTexCoord2d(1,1);  glVertex3d(-1,-1,1);
       glEnd();

       glBindTexture(GL_TEXTURE_2D, skytext[4]);

       glBegin(GL_QUADS);
       glTexCoord2d(0,1);  glVertex3d(1,-1,1);
       glTexCoord2d(0,0);  glVertex3d(1,1,1);
       glTexCoord2d(1,0);  glVertex3d(-1,1,1);
       glTexCoord2d(1,1);  glVertex3d(-1,-1,1);
       glEnd();

       glBindTexture(GL_TEXTURE_2D, skytext[5]);

       glBegin(GL_QUADS);
       glTexCoord2d(0,1);  glVertex3d(1,1,-1);
       glTexCoord2d(0,0);  glVertex3d(1,-1,-1);
       glTexCoord2d(1,0);  glVertex3d(-1,-1,-1);
       glTexCoord2d(1,1);  glVertex3d(-1,1,-1);
	   glEnd();

	   glDepthMask(GL_TRUE);
  	   //glEnable(GL_DEPTH_TEST);



}

string Skybox::toString(){
  string chaine;
  char buf[50];
  sprintf(buf,"%d:%d:%d:%d",position.getX(),position.getY(),position.getZ(),taille);
  chaine=buf;
  return chaine;

}
